﻿@{
    ViewBag.Title = "Form";
    Layout = "~/Views/Shared/_ManufactureList.cshtml";
}
<script type="text/javascript">
    var status = '@ViewBag.status';
    var number = '@ViewBag.number';
    var currRowId1 = 0;
    var currRowId2 = 0;
    $(function () {
        initControl();
        gridList();
    });
    function initControl() {
        if (status == 'I') {
            
        } else if (status == 'U') {
            var data = eval(@Html.Raw(ViewBag.oldEntity));
            for (var p in data) {
                $("#"+p).val(data[p]);
            }
            //$("#CombineNO").val(data.CombineNO);
            $("#CombineNO").prop("disabled", true);
            //$("#CombineName").val(data.CombineName);
            //$("#CombineName").prop("disabled", true);
        }
        $('select').bindSelect();
    }
    function gridList() {
        gridList1();
        gridList2();
    }
    function gridList1() {
        var $gridList = $("#gridList1");
        $gridList.dataGrid({
            url: "/BOM/Manufacture/GetProductNoCombineJson",
            postData: { combineNo: $('#CombineNO').val() },
            height: $(window).height() -160,
            colModel: [
                { label: '零件号', name: 'FProductionNO', width: 110, align: 'left', sortable: false },
                { label: '零件名称', name: 'FProductionName', width: 185, align: 'left', sortable: false }
            ],
            sortname: 'FProductionNO asc',
            viewrecords: true,  //是否显示行数
            rownumWidth: 40,
            rowNum:100000,
            gridComplete: function () {

            },
            loadComplete: function () {
                var ids = $gridList.jqGrid('getDataIDs');
                if (ids.length > 0) {
                    if (currRowId1 == 0) { currRowId1 = 1; }
                    $gridList.jqGrid('setSelection', currRowId1);
                }
            },
            onSelectRow: function (rowid) {
                currRowId1 = rowid;
            }
        });
        $gridList.jqGrid('setLabel', 0, '序号', 'rowNumberHeaderStyle');
        $("#btn_search").click(function () {
            reloadGrid();
        });
    }
    function gridList2() {
        var $gridList = $("#gridList2");
        $gridList.dataGrid({
            url: "/BOM/Manufacture/GetCombineProductJson",
            postData: { combineNo: $('#CombineNO').val() },
            height: $(window).height() - 160,
            colModel: [
                { label: '零件号', name: 'FProductionNO', width: 110, align: 'left', sortable: false },
                { label: '零件名称', name: 'FProductionName', width: 185, align: 'left', sortable: false },
                {
                    label: '数量', name: 'FProductionSum', width: 60, align: 'center', sortable: false, formatter: function (cellvalue, options, rowObject) {
                        return '<table class="form"><tr><td class="formValue" style="border:0;padding:0;margin:0;"><input value="' + cellvalue + '" id="FProductionSum_' + rowObject.FProductionNO + '" name="FProductionSum_' + rowObject.FProductionNO + '" class="form-control text-right digits" min="1" onkeyup="onlyDigital(this);" max="9" type="text" style="width:50px;"></td></tr></table>';
                    }
                }
            ],
            sortname: 'FProductionNO asc',
            viewrecords: true,  //是否显示行数
            rownumWidth: 40,
            rowNum: 100000,
            gridComplete: function () {

            },
            loadComplete: function () {
                var ids = $gridList.jqGrid('getDataIDs');
                if (ids.length > 0) {
                    currRowId2 = 1;
                    $gridList.jqGrid('setSelection', currRowId2);
                }
            },
            onSelectRow: function (rowid) {
                currRowId2 = rowid;
            }
        });
        $gridList.jqGrid('setLabel', 0, '序号', 'rowNumberHeaderStyle');
    }
    function btn_add() {
        if (currRowId1 > 0) {
            var rowData = $('#gridList1').jqGrid('getRowData', currRowId1);
            $('#gridList1').jqGrid('delRowData', currRowId1);
            var ids = $('#gridList1').jqGrid('getDataIDs');
            if (ids.length > 0) {
                currRowId1 = ids[0];
                $('#gridList1').jqGrid('setSelection', currRowId1);
            }
            var newRowData = { FProductionNO: rowData.FProductionNO, FProductionName: rowData.FProductionName, FProductionSum: 1 };
            var count = $("#gridList2").jqGrid("getGridParam", "records");
            var currRowId2 = count + 1;
            $('#gridList2').jqGrid('addRowData', currRowId2, newRowData, "last");
            $('#gridList2').jqGrid('setSelection', currRowId2);

            var lastTr = $("#gview_gridList2 tr:last");
            lastTr.focus();
            
        } else {
            $.modalAlert("请选择待添加的零件。", "warning");
        }
    }
    function btn_remove() {
        if (currRowId2 > 0) {
            var rowData = $('#gridList2').jqGrid('getRowData', currRowId2);
            $('#gridList2').jqGrid('delRowData', currRowId2);
            var ids = $('#gridList2').jqGrid('getDataIDs');
            if (ids.length > 0) {
                currRowId2 = ids[0];
                $('#gridList2').jqGrid('setSelection', currRowId2);
            }
            var newRowData = { FProductionNO: rowData.FProductionNO, FProductionName: rowData.FProductionName };
            var count = $("#gridList1").jqGrid("getGridParam", "records");
            var currRowId1 = count + 1;
            $('#gridList1').jqGrid('addRowData', currRowId1, newRowData, "last");
            $('#gridList1').jqGrid('setSelection', currRowId1);

            var lastTr = $("#gview_gridList1 tr:last");
            lastTr.focus();
        } else {
            $.modalAlert("请选择待移除的零件。", "warning");
        }
    }
    function reloadGrid() {
        var idsArray = [];
        var ids = $('#gridList2').jqGrid('getDataIDs');
        for (var i = 0; i < ids.length; i++) {
            var rowData = $('#gridList2').jqGrid('getRowData', ids[i]);
            idsArray.push(rowData.FProductionNO);
        }
        $("#gridList1").jqGrid('setGridParam', {
            postData: { keyword: $("#txt_keyword").val(), extIds: idsArray.join(",") },
        }).trigger('reloadGrid');
    }

    function btn_ok() {
        var ids = $('#gridList2').jqGrid('getDataIDs');
        if (ids.length > 0) {
            submitForm();
        } else {
            $.modalAlert("请选择套裁合件。", "warning");
        }
    }
    function submitForm() {
        if (!$('#form1').formValid()) {
            return false;
        }
        var data = [];
        var ids = $('#gridList2').jqGrid('getDataIDs');
        for (var i = 0; i < ids.length; i++) {
            var rowData = $('#gridList2').jqGrid('getRowData',ids[i]);
            var item = { CombineNO: $('#CombineNO').val(), FProductionNO: rowData.FProductionNO, FProductionSum: $('#FProductionSum_' + rowData.FProductionNO).val() };
            data.push(item);
        }
        var param = $("#form1").formSerialize();        param["listContent"] = JSON.stringify(data);
        $.submitForm({
            url: "/BOM/Manufacture/SubmitStampingKitInputForm?status=" + status,
            param: param,
            success: function () {
                parent.reloadGrid();
                parent.$.detailClose();
            }
        })
    }
    function btn_cancel() {
        parent.$.detailClose();
    }
</script>
<div class="ui-layout" id="layout" style="width: 998px;margin:0 auto">
    <form id="form1">
        <div style="width: 900px; text-align: right; margin: 15px; border-bottom: 2px solid #ccc;">
            <table class="form" style="">
                <tr>
                    <td class="formValue" width="160px;">
                        <div class="btn-group dialog-title">
                            @(ViewBag.status == "U" ? "修改" : "新增")冲压合件信息
                        </div>
                    </td>
                    <th class="formTitle" style="width:60px;">合件编号</th>
                    <td class="formValue input-width-120" style="width:120px;">
                        <input id="CombineID" name="CombineID" type="hidden" value="" />
                        <input id="CombineNO" name="CombineNO" type="text" class="form-control required" value="" />
                    </td>
                    <th class="formTitle" style="width:70px;">合件名称</th>
                    <td class="formValue input-width-240" style="width:240px;">
                        <input id="CombineName" name="CombineName" type="text" class="form-control required" value="" />
                    </td>
                    <td style="padding: 5px 10px;">
                        <div class="btn-group" style="margin-right:20px;">
                            <a id="btn_ok" class="btn btn-primary center-block" onclick="btn_ok()"><span class="glyphicon glyphicon-ok"></span> 确认</a>
                        </div>
                        <div class="btn-group">
                            <a id="btn_cancel" class="btn btn-primary center-block" onclick="btn_cancel()"><span class="glyphicon glyphicon-remove"></span> 取消</a>
                        </div>
                    </td>
                </tr>
            </table>
        </div>
        <div style="width: 900px; padding: 0 0; margin:0 0 0 15px;">
            <div style="width:360px;float:left;border:1px solid #ccc;">
                <div style="padding:5px 10px" class="line-table input-width-140">
                    <div class="btn-group dialog-title" style="margin:0">
                        零件目录
                    </div>
                    <div class="btn-group" style="">
                        零件<input id="txt_keyword" name="" type="text" class="form-control" />&nbsp;
                    </div>
                    <div class="btn-group">
                        <a id="btn_search" class="btn btn-primary center-block btn-1" onclick="btn_search()"><span class="glyphicon glyphicon-search"></span> 检索</a>
                    </div>
                </div>
                <table id="gridList1"></table>
            </div>
            <div style="width:100px;float:left;padding:0 10px;">
                <a id="btn_add" class="btn btn-primary center-block" onclick="btn_add()" style="margin-top:80px;"><span class="glyphicon glyphicon-plus"></span> &nbsp;添加</a>
                <a id="btn_remove" class="btn btn-primary center-block" onclick="btn_remove()" style="margin-top:80px;"><span class="glyphicon glyphicon-trash"></span> &nbsp;移除</a>
            </div>
            <div style="width: 420px; float: left; border: 1px solid #ccc;">
                <div style="padding:5px 10px" class="line-table input-width-140">
                    <div class="btn-group dialog-title" style="margin:0">
                        合件包含零件
                    </div>
                </div>               
                <table id="gridList2"></table>
            </div>
        </div>
    </form>
</div>

